//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AnalyticPTDHestonEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Models/Equity/PiecewiseTimeDependentHestonModel.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Models::Equity;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (Cephei::QL::Models::Equity::IPiecewiseTimeDependentHestonModel^ model, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationOrder) : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
    CPiecewiseTimeDependentHestonModel^ _Cmodel;
    try
    {
#ifdef HANDLE
        _phAnalyticPTDHestonEngine = NULL;
#endif
        _Cmodel = safe_cast<CPiecewiseTimeDependentHestonModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::PiecewiseTimeDependentHestonModel>& _model = static_cast<boost::shared_ptr<QuantLib::PiecewiseTimeDependentHestonModel>&> (_Cmodel->GetShared ()); 
        QuantLib::Size _integrationOrder = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (integrationOrder) ? (QuantLib::Size)ValueHelper::Convert (integrationOrder->Value) : 144); //4
        _ppAnalyticPTDHestonEngine = new boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine> (new QuantLib::AnalyticPTDHestonEngine ( _model,  _integrationOrder ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (Cephei::QL::Models::Equity::IPiecewiseTimeDependentHestonModel^ model, Double relTolerance, UInt64 maxEvaluations) : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
    CPiecewiseTimeDependentHestonModel^ _Cmodel;
    try
    {
#ifdef HANDLE
        _phAnalyticPTDHestonEngine = NULL;
#endif
        _Cmodel = safe_cast<CPiecewiseTimeDependentHestonModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::PiecewiseTimeDependentHestonModel>& _model = static_cast<boost::shared_ptr<QuantLib::PiecewiseTimeDependentHestonModel>&> (_Cmodel->GetShared ()); 
        QuantLib::Real _relTolerance = (QuantLib::Real)ValueHelper::Convert (relTolerance); //d
        QuantLib::Size _maxEvaluations = (QuantLib::Size)ValueHelper::Convert (maxEvaluations); //d
        _ppAnalyticPTDHestonEngine = new boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine> (new QuantLib::AnalyticPTDHestonEngine ( _model,  _relTolerance,  _maxEvaluations ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine>& childNative, Object^ owner) : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticPTDHestonEngine = NULL;
#endif
	_ppAnalyticPTDHestonEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (QuantLib::AnalyticPTDHestonEngine& childNative, Object^ owner) : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticPTDHestonEngine = NULL;
#endif
	_ppAnalyticPTDHestonEngine = new boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
    _AnalyticPTDHestonEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (CAnalyticPTDHestonEngine^ copy) : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticPTDHestonEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticPTDHestonEngine = new boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (PLATFORM::Type^ t) : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticPTDHestonEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticPTDHestonEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (QuantLib::Handle<QuantLib::AnalyticPTDHestonEngine>& childNative, Object^ owner)  : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
	_phAnalyticPTDHestonEngine = &childNative;
	_ppAnalyticPTDHestonEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
    _AnalyticPTDHestonEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (QuantLib::Handle<QuantLib::AnalyticPTDHestonEngine> childNative)  : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
	_phAnalyticPTDHestonEngine = &childNative;
	_ppAnalyticPTDHestonEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::CAnalyticPTDHestonEngine (QuantLib::AnalyticPTDHestonEngine childNative)  : CPricingEngine(CAnalyticPTDHestonEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticPTDHestonEngine = NULL;
#endif
	_ppAnalyticPTDHestonEngine = new boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine> (new QuantLib::AnalyticPTDHestonEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticPTDHestonEngine));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::~CAnalyticPTDHestonEngine ()
{
    if (_ppAnalyticPTDHestonEngine != NULL)
    {
	    delete _ppAnalyticPTDHestonEngine;
        _ppAnalyticPTDHestonEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::!CAnalyticPTDHestonEngine ()
{
    if (_ppAnalyticPTDHestonEngine != NULL)
    {
	    delete _ppAnalyticPTDHestonEngine;
    }
}
QuantLib::AnalyticPTDHestonEngine& Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::GetReference ()
{
    if (_ppAnalyticPTDHestonEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppAnalyticPTDHestonEngine;
}
boost::shared_ptr<QuantLib::AnalyticPTDHestonEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::GetShared ()
{
    if (_ppAnalyticPTDHestonEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppAnalyticPTDHestonEngine;
}
QuantLib::AnalyticPTDHestonEngine* Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::GetPointer ()
{
    if (_ppAnalyticPTDHestonEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppAnalyticPTDHestonEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticPTDHestonEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::GetHandle ()
{
	if (_phAnalyticPTDHestonEngine == NULL)
	{
		_phAnalyticPTDHestonEngine = new Handle<QuantLib::AnalyticPTDHestonEngine> (*_ppAnalyticPTDHestonEngine);
	}
	return *_phAnalyticPTDHestonEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::HasNative () 
{
	return (_ppAnalyticPTDHestonEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IAnalyticPTDHestonEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticPTDHestonEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IAnalyticPTDHestonEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine_Factory::Create (Cephei::QL::Models::Equity::IPiecewiseTimeDependentHestonModel^ model, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationOrder)
{
    return REFNEW CAnalyticPTDHestonEngine ( model,  integrationOrder);
}
Cephei::QL::Pricingengines::Vanilla::IAnalyticPTDHestonEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticPTDHestonEngine_Factory::Create (Cephei::QL::Models::Equity::IPiecewiseTimeDependentHestonModel^ model, Double relTolerance, UInt64 maxEvaluations)
{
    return REFNEW CAnalyticPTDHestonEngine ( model,  relTolerance,  maxEvaluations);
}
